package com.elitesland.system.rest;

import com.elitesland.core.base.ApiCode;
import com.elitesland.core.base.ApiResult;
import com.elitesland.core.base.PagingVO;
import com.elitesland.system.entity.SysUserDTO;
import com.elitesland.system.param.SysPasswordUpdate;
import com.elitesland.system.param.SysUserNewParam;
import com.elitesland.system.param.SysUserQueryParam;
import com.elitesland.system.param.SysUserUpdateParam;
import com.elitesland.system.service.SysUserService;
import com.elitesland.system.vo.SysPermissionVO;
import com.elitesland.system.vo.SysUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <pre>
 * 系统用户账号维护接口
 * </pre>
 *
 * @author Mir
 * @date 2020/6/15
 */
@Slf4j
@RestController
@RequestMapping("/sys/users")
@RequiredArgsConstructor
@Api(value = "系统用户账号维护", tags = {"系统用户账号维护"})
public class SysUserController {

    private final SysUserService sysUserService;

    @PostMapping("/q")
    @ApiOperation("检索的用户账号信息")
    @ApiResponses(
            @ApiResponse(code = 200, response = SysUserVO.class, message = "处理成功")
    )
    public ApiResult<PagingVO<SysUserVO>> search(@RequestBody SysUserQueryParam param) {
        val ret = sysUserService.search(param);
        return ApiResult.ok(ret);
    }

    @PostMapping
    @ApiOperation("创建用户账号")
    public ApiResult<Long> create(@RequestBody SysUserNewParam user) {
        val id = sysUserService.create(user);
        return ApiResult.ok(id);
    }

    @GetMapping("/{id}")
    @ApiOperation("通过ID取得用户明细信息")
    public ApiResult<SysUserDTO> one(@PathVariable Long id) {
        return sysUserService.getById(id).map(ApiResult::ok)
                .orElse(ApiResult.fail(ApiCode.NOT_FOUND));
    }

    @GetMapping("/current")
    @ApiOperation("获取当前用户信息")
    public ApiResult<?> current() {
        return sysUserService.current().map(ApiResult::ok)
                .orElse(ApiResult.fail(ApiCode.NOT_FOUND));
    }

    @GetMapping("/current/menus")
    @ApiOperation("获取当前用户的菜单清单")
    public ApiResult<List<SysPermissionVO>> currentMenus() {
        return ApiResult.ok(sysUserService.currentMenu());
    }

    @GetMapping("/current/actions")
    @ApiOperation("获取当前用户的能力清单")
    public ApiResult<Set<SysPermissionVO>> currentActions() {
        return ApiResult.ok(sysUserService.currentActions());
    }

    @GetMapping("/current/menus/{menuId}")
    @ApiOperation("获取当前用户，当前菜单页面的能力清单")
    public ApiResult<Set<SysPermissionVO>> currentMenuActions(@PathVariable Long menuId) {
        return ApiResult.ok(sysUserService.currentMenuActions(menuId));
    }

    @PutMapping
    @ApiOperation("更新用户信息")
    public ApiResult<Boolean> update(@RequestBody @Validated SysUserUpdateParam user) {
        sysUserService.update(user);
        return ApiResult.ok(true);
    }

    @PutMapping("/p/{userId}/{password}")
    @ApiOperation("修改密码-临时方案，不安全")
    public ApiResult<Object> changePasswordNoSec(@PathVariable Long userId, @PathVariable String password) {
        sysUserService.changePasswordNoSec(userId, password);
        return ApiResult.ok();
    }

    @PutMapping("/{id}")
    @ApiOperation("切换用户启用状态")
    public ApiResult<Boolean> switchUserStatus(@PathVariable Long id) {
        val ret = sysUserService.switchUserStatus(id);
        return ApiResult.ok(ret);
    }

    @PutMapping("/changep")
    @ApiOperation("修改用户密码")
    public ApiResult<Object> updatePassword(@RequestBody SysPasswordUpdate update) {
        sysUserService.updatePassword(update);
        return ApiResult.ok();
    }
}
